//
//  CYOlineScanVC.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/9/25.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//
#define offsetX AL(PT(80))
#import "CYOnlineScanVC.h"
#import "CYMoneySeg.h"
#import "CYQRCode.h"
@interface CYOnlineScanVC(){
    olineScan _myblock;
}
@property (nonatomic, strong) CYMoneySeg *moneyseg;//充值金额选择
@property (nonatomic, strong) CYButton *rechargeBtn;//立即充值
@property (nonatomic, copy) NSString *money;//金额
@property (nonatomic, strong) CYLabel *orderLBL;//订单号
@property (nonatomic, strong) CYLabel *moneyLBL;//金额
@property (nonatomic, strong) CYImageView *QRIMG;//二维码
@property (nonatomic, strong) CYButton *saveBtn;//保存二维码
@property (nonatomic, strong) CYButton *jumpBtn;//跳转按钮
@property (nonatomic, assign) int payInfoId;//接口id
@property (nonatomic, copy) NSString *title;//
@end
@implementation CYOnlineScanVC
- (instancetype)initWithTitle:(NSString *)title block:(olineScan)block{
    self = [super init];
    if (self) {
        _myblock = block;
        _title = title;
        self.moneyseg.hidden = NO;
        self.rechargeBtn.hidden = NO;
    }
    return self;
}
- (CYMoneySeg *)moneyseg{
    if (!_moneyseg) {
        WS(self)
        _moneyseg = [[CYMoneySeg alloc]initWithBlock:^(NSString * number) {
            weakSelf.money = number;
        }];
        _moneyseg.title = @"充值金额(范围1.000~10000.00)";
        [self addSubview:_moneyseg];
        
        [_moneyseg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(_moneyseg.bounds.size);
        }];
    }
    return _moneyseg;
}

- (CYButton *)rechargeBtn{
    if (!_rechargeBtn) {
        _rechargeBtn = [[CYButton alloc]initWithTitle:@"提交充值" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_rechargeBtn addTarget:self action:@selector(actionForRechargeBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rechargeBtn];
        
        [_rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_moneyseg.mas_bottom).with.offset(AL(PT(100)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - offsetX * 2) , AL(44)));
        }];
    }
    return _rechargeBtn;
    
}

- (CYLabel *)orderLBL{
    if (!_orderLBL) {
        _orderLBL = [[CYLabel alloc]initWithText:@"订单号:" size:AL(PT(28)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_orderLBL];
        
        [_orderLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(AL(PT(10)));
            make.left.equalTo(self).with.offset(AL(PT(20)));
            make.size.mas_equalTo(_orderLBL.bounds.size);
        }];
    }
    return _orderLBL;
}

- (CYLabel *)moneyLBL{
    if (!_moneyLBL) {
        _moneyLBL = [[CYLabel alloc]initWithText:@"金额:" size:AL(PT(28)) textcolor:TEXT_BLACK_COLOR];
        [self addSubview:_moneyLBL];
        
        [_moneyLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.orderLBL.mas_bottom).with.offset(AL(PT(10)));
            make.left.equalTo(self.orderLBL);
            make.size.mas_equalTo(_moneyLBL.bounds.size);
        }];
    }
    return _moneyLBL;
}

- (CYImageView *)QRIMG{
    if (!_QRIMG) {
        _QRIMG = [[CYImageView alloc]init];
        [self addSubview:_QRIMG];
        
        [_QRIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.moneyLBL.mas_bottom).with.offset(AL(PT(20)));
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 2.0, SCREEN_WIDTH / 2.0));
        }];
    }
    return _QRIMG;
}
- (CYButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [[CYButton alloc]initWithTitle:@"保存二维码" titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_saveBtn addTarget:self action:@selector(actionForSaveBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_saveBtn];
        
        [_saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.QRIMG.mas_bottom).with.offset(AL(PT(20)));
            make.left.equalTo(self).with.offset(AL(PT(40)));
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - AL(PT(40)) * 2.5) / 2.0, AL(44)));
        }];
    }
    return _saveBtn;
}

- (CYButton *)jumpBtn{
    if (!_jumpBtn) {
        _jumpBtn = [[CYButton alloc]initWithTitle:[NSString stringWithFormat:@"跳转%@",_title] titleColor:[UIColor whiteColor] backgroundColor:THEME_COLOR cornerRadius:5.0];
        [_jumpBtn addTarget:self action:@selector(actionForJumpBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_jumpBtn];
        
        [_jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.QRIMG.mas_bottom).with.offset(AL(PT(20)));
            make.right.equalTo(self).with.offset(- AL(PT(40)));
            make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - AL(PT(40)) * 2.5) / 2.0, AL(44)));
        }];
    }
    return _jumpBtn;
}

- (void)setModel:(CYRechergeModel *)model{
    if (_model != model) {
        _model = model;
        _moneyseg.title = [NSString stringWithFormat:@"充值金额(范围%.2f~%.2f)",_model.minQuota, _model.maxQuota];
    }
}
- (void)showQRCodeWithXMLModel:(CYThirdXMLModel *)xmlModel{
    if (xmlModel.resultCode != 0){//失败
        [CYHUD show:xmlModel.resultDesc];
        return;
    }
    self.moneyseg.hidden = self.rechargeBtn.hidden = YES;
    self.orderLBL.text = [NSString stringWithFormat:@"订单号:%@",xmlModel.orderNo];
    self.moneyLBL.text = [NSString stringWithFormat:@"金额:%d元",xmlModel.orderAmount];
    self.QRIMG.image = [CYQRCode QRCodeWithUrlString:xmlModel.qrcode];
    self.saveBtn.hidden = self.jumpBtn.hidden = NO;
    [self updateOrderLBLBounds];
    [self updateMoneyLBLBounds];

}

- (void)updateOrderLBLBounds{
    [_orderLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_orderLBL.bounds.size);
    }];
}

- (void)updateMoneyLBLBounds{
    [_moneyLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(_moneyLBL.bounds.size);
    }];
}
//保存到相册
- (void)actionForSaveBtn{
    _myblock(saveForOnlineScanEvent,_QRIMG.image);
}

//跳转
- (void)actionForJumpBtn{
    _myblock(jumpForOnlineScanEvent,nil);
    
}
//提交充值
- (void)actionForRechargeBtn{
    if (!_money)
        [CYHUD show:@"请输入正确金额"];
    else
        if (_delegate && [_delegate respondsToSelector:@selector(httpsWithMoney:)]) {
            [_delegate httpsWithMoney:_money];
        }
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
